$(function() {
  $.ajax({
    type: "get",
    url: "goods/detail",
    data: { goods_id: getid("goods_id") },
    dataType: "json",
    success: function(res) {
      console.log(res);
      返回参数 = res.data;
      $(".内容").html(template("template1", res.data));
      初始化轮播图();
    }
  });

  function getid(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return decodeURI(r[2]);
    return null;
  }
  function 初始化轮播图() {
    var gallery = mui(".mui-slider");
    gallery.slider({
      interval: 5000 //自动轮播周期，若为0则不自动播放，默认为0；
    });
  }
  function 询问是否跳转() {
    mui.confirm("添加成功", "否是跳转到结算页面", ["是", "否"], function (
      a
    ) {
      if (a.index == 0) {
        window.location.href = '../index2/购物车.html'
      } else if (a.index == 1) {
        console.log('取消');
      }
    });
  }

 

  $("#加入购物车").on("tap", function() {
    var token1 = sessionStorage.getItem("用户登录数据");
    if (token1) {
      购物车参数 = {
        cat_id: 返回参数.cat_id,
        goods_id: 返回参数.goods_id,
        goods_name: 返回参数.goods_name,
        goods_number: 返回参数.goods_number,
        goods_price: 返回参数.goods_price,
        goods_small_logo: 返回参数.goods_small_logo,
        goods_weight: 返回参数.goods_weight
      };
      var 购物车参数对象 = JSON.stringify(购物车参数);
      var token = JSON.parse(token1).token;
      $.ajax({
        type: "post",
        url: "my/cart/add",
        headers: {
          Authorization: token
        },
        data: { info: 购物车参数对象 },
        dataType: "json",
        success: function(res) {
          console.log(res);
        }
      });
      询问是否跳转()
    } else {
      sessionStorage.setItem("跳转路径", location.href);
      mui.toast("请先登录");
      setTimeout(function() {
        window.location.href = "../index2/登录页面.html";
      }, 2000);
    }
  });
});
